package cn.scantd.kit;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import jodd.io.StringInputStream;
import jodd.io.StringInputStream.Mode;

/**
 * 文件工具类
 * 
 * @author xuq
 */
public class FileKit {
    
    public static final int BUFFER_SIZE = 1024;

    /**
     * 判断目标路径是否为文件夹
     * 
     * @param target
     *            目标路径
     * @return 是否为文件夹
     */
    public static boolean isDic(String target) {
        if (StrKit.isBlank(target)) {
            return false;
        }
        return isDic(new File(target));
    }

    /**
     * 判断目标路劲是否为文件夹
     * 
     * @param target
     *            目标路径
     * @return 是否为文件夹
     */
    public static boolean isDic(File target) {
        if (target == null) {
            return false;
        }
        if (target.exists() && target.isDirectory()) {
            return true;
        }
        return false;
    }

    /**
     * 判断目标路径是否为文件
     * 
     * @param target
     *            目标路径
     * @return 是否为文件
     */
    public static boolean isFile(String target) {
        if (target == null) {
            return false;
        }
        return isFile(new File(target));
    }

    /**
     * 判断目标是否为文件
     * 
     * @param target
     *            目标
     * @return 是否为文件
     */
    public static boolean isFile(File target) {
        if (target == null) {
            return false;
        }
        if (target.exists() && target.isFile()) {
            return true;
        }
        return false;
    }

    /**
     * 获取目标文件的后缀名
     * 
     * @param target
     *            目标文件
     * @return 后缀名
     */
    public static String getExtension(File target) {
        if (target == null) {
            return null;
        }
        if (isFile(target)) {
            int index = -1;
            String fileName = target.getName();
            if ((index = fileName.indexOf(".")) > -1) {
                return fileName.substring(index);
            }
            return null;
        }
        return null;
    }

    /**
     * 列出该路径下的文件
     * 
     * @param target
     *            目标路径
     * @param filter
     *            过滤条件
     * @return 文件数组
     */
    public static File[] listFile(String target, FileFilter filter) {
        if (isDic(target)) {
            File dic = new File(target);
            File[] files = dic.listFiles(filter);
            return files;
        }
        throw new RuntimeException("Target is not a dictory but a file! Please choose a dictory to scan!");
    }

    /**
     * 递归获取匹配的所有文件
     * 
     * @param files
     *            获取的结果集
     * @param target
     *            目标文件
     * @param filter
     *            过滤条件
     */
    public static void traceListFile(List<File> files, String target, FileFilter filter) {
        File[] root = listFile(target, null);
        for (File file : root) {
            if (FileKit.isDic(file)) {
                traceListFile(files, file.getAbsolutePath(), filter);
            } else {
                if (filter.accept(file)) {
                    files.add(file);
                }
            }
        }
    }

    /**
     * 读取文件内容
     * 
     * @param file 文件
     * @return 文件内容
     */
    public static byte[] readFromFile(File file) {
        byte[] input = null;
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
            input = new byte[bufferedInputStream.available()];
            bufferedInputStream.read(input);
            bufferedInputStream.close();
            
            return input;
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    /**
     * 将内容写入到文件
     * 
     * @param file 目标文件
     * @param content 内容
     */
    public static void writeToFile(File file, String content) {
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new StringInputStream(content, Mode.ASCII));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
            byte[] buffer = new byte[BUFFER_SIZE];
            int length = 0;
            while((length = bufferedInputStream.read(buffer)) != -1) {
                bufferedOutputStream.write(buffer, 0, length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedOutputStream.close();
                bufferedInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }

    /**
     * 讲包结构转为物理文件夹结构
     * 
     * @param packageName 包名
     * @return 目标文件夹地址
     */
    public static String convertToDic(String packageName) {
        packageName = packageName.replaceAll("\\.", File.separator);
        return packageName;
    }
    
}
